package com.ruoyi.project.business.askleave.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.business.common.enums.HolidayType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.askleave.mapper.AskLeaveMapper;
import com.ruoyi.project.business.askleave.entity.AskLeave;
import com.ruoyi.project.business.askleave.request.ListAskLeaveRequest;
import com.ruoyi.project.business.askleave.request.AddAskLeaveRequest;
import com.ruoyi.project.business.askleave.response.AskLeaveResponse;
import com.ruoyi.project.business.askleave.service.IAskLeaveService;

/**
 * 请假Service业务层处理
 * 
 * @author ${author}
 * @date 2024-04-11
 */
@Service
public class AskLeaveServiceImpl implements IAskLeaveService {
    @Autowired
    private AskLeaveMapper askLeaveMapper;

    /**
     * 查询请假
     * 
     * @return 请假
     */
    @Override
    public AskLeave selectAskLeaveById(Long id) {
        return askLeaveMapper.selectAskLeaveById(id);
    }

    /**
     * 查询请假列表
     * 
     * @return 请假
     */
    @Override
    public List<AskLeaveResponse> selectAskLeaveList(ListAskLeaveRequest request, Long tenantId) {
        AskLeave param = BeanUtils.copy(request, AskLeave.class);

        Date yearMonth = request.getYearMonth();
        param.setYear(DateUtils.year(yearMonth));
        param.setMonth(DateUtils.month(yearMonth));
        param.setTenantId(tenantId);
        List<AskLeave> entitis = askLeaveMapper.selectAskLeaveList(param);
        List<AskLeaveResponse> resList = BeanUtils.copyList(entitis, AskLeaveResponse.class);
        return resList;
    }

    /**
     * 新增请假
     * 
     * @return 结果
     */
    @Override
    public int insertAskLeave(AddAskLeaveRequest request, Long operatorId, Long tenantId) {
        AskLeave entity = BeanUtils.copy(request, AskLeave.class);
        LocalDate now = LocalDate.now();

        entity.setHolidayName(HolidayType.valueOf(request.getHolidayType()).getDesc());
        entity.setMonth(now.getMonthValue());
        entity.setYear(now.getYear());
        entity.setDayOfMonth(now.getDayOfMonth());
        entity.setDayOfWeek(now.getDayOfWeek().getValue());
        entity.setTenantId(tenantId);
        entity.setCreator(operatorId);
        entity.setDeleted(Boolean.FALSE);
        entity.setCreateTime(DateUtils.getNowDate());
        return askLeaveMapper.insertAskLeave(entity);
    }

    /**
     * 修改请假
     * 
     * @return 结果
     */
    @Override
    public int updateAskLeave(AskLeave entity, Long operatorId, Long tenantId) {
        entity.setUpdateTime(DateUtils.getNowDate());
        entity.setTenantId(tenantId);
        entity.setUpdater(operatorId);
        return askLeaveMapper.updateAskLeave(entity);
    }

    /**
     * 批量删除请假
     * 
     * @return 结果
     */
    @Override
    public int deleteAskLeaveByIds(Long[] ids, Long operatorId, Long tenantId) {
        return askLeaveMapper.deleteAskLeaveByIds(ids);
    }

    /**
     * 删除请假信息
     * 
     * @return 结果
     */
    @Override
    public int deleteAskLeaveById(Long id, Long operatorId, Long tenantId) {
        return askLeaveMapper.deleteAskLeaveById(id);
    }

    @Override
    public List<AskLeave> findUserLeaves(List<Integer> aimDayOfMonths, Integer year, Integer month, Long userId, Long tenantId) {
        List<AskLeave> leaves = askLeaveMapper.findUserLeaves(aimDayOfMonths, year, month, userId, tenantId);
        return leaves;
    }


}
